//
//  BookMineViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/6.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//

#import "BookMineViewController.h"
#import "BookhuiyuanCenterViewController.h"
#import "BookScoreMarketViewController.h"
#import "LXMyScoreViewController.h"
#import "LXRequstContentViewController.h"
#import "BkMyPublishViewController.h"
#import "MySavedViewController.h"
#import "MyInvateViewController.h"
#import "MyServerTelViewController.h"
#import "LXSettingViewController.h"
#import "LXPersionalViewController.h"
#import "LXMessageViewController.h"

#import "LXUserInforModel.h"
#import "LXSaveUserInforTool.h"
#import "DSMineBackView.h"
#import "BookItemsView.h"
#import "BookOrderView.h"
#import "BookMoneyAccountView.h"
@interface BookMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) DSMineBackView *headerView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSArray *imgArray;
@property (nonatomic,strong) UIView *tableHeaderView;
@property (nonatomic,strong) BookOrderView *orderView;
@property (nonatomic,strong) BookMoneyAccountView *accountView;
@property (nonatomic,strong) BookItemsView *huiyuanView;
@property (nonatomic,strong) BookItemsView *jifenView;

@end

@implementation BookMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = kSubBacColor;
//    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:self.messageBtn];
//    self.navigationItem.rightBarButtonItem = rightItem;
    self.orderView.wulliuArray = @[];
}


-(void)messageAction:(UIButton *)sender
{
    LXMessageViewController *vc = [[LXMessageViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ----获取未读消息数量

-(void)requstUnreadCount{
    //getmessnum
    [NetWorkTools postConJSONWithUrl:@"getmessnum" parameters:@{} success:^(id responseObject) {
       
        NSString *datastr = responseObject[@"datastr"];
//        if ([result integerValue] == 0) {
//            self.classify1Array = dataList;
//        }else{
//            [MBProgressHUD showError:resultNote];
//        }
        UIView *v = [self.headerView.messageBtn viewWithTag:22000000];
        if (v) {
            [v removeFromSuperview];
        }
        if (datastr.intValue !=0) {
           
            UILabel *unreadLabel = [TSWLTools allocLabel:datastr font:systemFont(TSScaleW(8)) textColor:kWhiteColor frame:CGRectMake(TSScaleW(30), TSScaleW(5), TSScaleW(18), TSScaleW(12)) textAlignment:(NSTextAlignmentCenter)];
            unreadLabel.tag = 22000000;
            [self.headerView.messageBtn addSubview:unreadLabel];
            unreadLabel.backgroundColor = kRedColor;
            unreadLabel.cornerRadius = unreadLabel.height/2.f;
           
            CGFloat w = [datastr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, TSScaleW(10)) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:systemFont(TSScaleW(8))} context:nil].size.width;
            unreadLabel.width = w + TSScaleW(8);
            unreadLabel.left = self.headerView.messageBtn.width - unreadLabel.width;
        }
    } fail:^(NSError *error) {
        
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.headerView.type = kLogin?MineStatusTypeLogin:MineStatusTypeLoginOut;
    [self userInforRequstAction];
    [self requstUnreadCount];
   
}

-(DSMineBackView *)headerView{
    if (!_headerView) {
        _headerView = [[DSMineBackView alloc]init];
        WS(weakSelf);
        _headerView.headerBlock = ^{
            if ([weakSelf ifNoLoginGotoLoginAction]) {
                LXPersionalViewController *vc = [[LXPersionalViewController alloc]init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
           }
            
        };
        _headerView.settingsBlock = ^{
            LXSettingViewController *vc = [[LXSettingViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        _headerView.messageBlock = ^{
            LXMessageViewController *vc = [[LXMessageViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
    }
    return _headerView;
}
-(UIView *)tableHeaderView{
    if (!_tableHeaderView) {
        _tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.width, TSScaleW(200))];
        [_tableHeaderView addSubview:self.orderView];
    
        [_tableHeaderView addSubview:self.accountView];
        [_tableHeaderView addSubview:self.huiyuanView];
        [_tableHeaderView addSubview:self.jifenView];
        _tableHeaderView.height = self.jifenView.bottom + TSScaleW(10);
    }
    return _tableHeaderView;
}
-(BookOrderView *)orderView
{
    if (!_orderView) {
        _orderView = [[BookOrderView alloc]init];
        WS(weakSelf);
        _orderView.orderSelectBlock = ^(NSString * _Nonnull title) {
            if (![weakSelf ifNoLoginGotoLoginAction]) {
                return;
            }
            LXRequstContentViewController *vc = [[LXRequstContentViewController alloc]init];
            vc.statusString = title;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        _orderView.seeAllOrderBlock = ^{
            if (![weakSelf ifNoLoginGotoLoginAction]) {
                return;
            }
            LXRequstContentViewController *vc = [[LXRequstContentViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        _orderView.reloadFrameBlock = ^{
            weakSelf.tableHeaderView = nil;
            weakSelf.accountView = nil;
            weakSelf.huiyuanView = nil;
            weakSelf.jifenView = nil;
            weakSelf.tableView.tableHeaderView = weakSelf.tableHeaderView;
        };
        
    }
    return _orderView;
}
-(BookMoneyAccountView *)accountView{
    if (!_accountView) {
        _accountView = [[BookMoneyAccountView alloc]init];
        _accountView.top = TSScaleW(12) + _orderView.bottom;
        NSString *state = [LXSaveUserInforTool sharedUserTool].status;
         if (!state.integerValue) {
             _accountView.hidden = YES;
         }else{
             _accountView.hidden = NO;
         }
        WS(weakSelf);
        _accountView.gotoBlock = ^{
            if (![weakSelf ifNoLoginGotoLoginAction]) {
                return;
            }
            [weakSelf requstOnOff];
          
        };
    }
    return _accountView;
}
-(void)requstOnOff{
 
    [NetWorkTools postConJSONWithUrl:@"getiosbuttons" parameters:@{} success:^(id responseObject) {
       
        NSString *state = responseObject[@"state"];
        LXMyScoreViewController *vc = [[LXMyScoreViewController alloc]init];
        vc.isOn = !state.integerValue;
        [self.navigationController pushViewController:vc animated:YES];
       
    } fail:^(NSError *error) {
              
    }];
}
-(BookItemsView *)huiyuanView{
    if (!_huiyuanView) {
        NSString *state = [LXSaveUserInforTool sharedUserTool].status;
        CGFloat top = TSScaleW(12) + _orderView.bottom;
        if (state.integerValue == 0) {
            top = TSScaleW(12) + _orderView.bottom;
        }else{
            top =  _accountView.bottom + TSScaleW(10);
        }
        _huiyuanView = [[BookItemsView alloc]initWithTop:top x:0 name:@"会员中心" subName:@"享受更大优惠" bgImg:@"huiyuanbg"];
        WS(weakSelf);
        _huiyuanView.gotoBlock = ^{
            [weakSelf gotoWebWithUrl:kUserDelegateUrlTag(@"4") title:@"会员中心"];
        };
         if (!state.integerValue) {
             _huiyuanView.hidden = YES;
         }else{
             _huiyuanView.hidden = NO;
         }
    }
    return _huiyuanView;
}
-(BookItemsView *)jifenView{
    if (!_jifenView) {
        NSString *state = [LXSaveUserInforTool sharedUserTool].status;
        CGFloat top = TSScaleW(12) + _orderView.bottom;
        if (state.integerValue == 0) {
            top = TSScaleW(12) + _orderView.bottom;
        }else{
            top =  _accountView.bottom + TSScaleW(10);
        }
       
        CGFloat x = _huiyuanView.right + TSScaleW(7);
        if (state.integerValue == 0) {
            x = 0;
        }else{
            x =  _huiyuanView.right + TSScaleW(7);
        }
        _jifenView = [[BookItemsView alloc]initWithTop:top x:x name:@"积分商城" subName:@"更多好货推荐" bgImg:@"huiyuanbg"];
        
        WS(weakSelf);
        _jifenView.gotoBlock = ^{
            if (![weakSelf ifNoLoginGotoLoginAction]) {
                return;
            }
            BookScoreMarketViewController *vc = [[BookScoreMarketViewController alloc]init];
            [weakSelf naviPushVc:vc];
        };
    }
    return _jifenView;
}
-(NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *state = [LXSaveUserInforTool sharedUserTool].status;
        NSArray *array = @[@"我的发布",@"我的收藏",@"我的邀请",@"客服热线"];
        if (state.integerValue == 0) {
            array = @[@"我的发布",@"我的收藏",@"客服热线"];
        }else{
            array = @[@"我的发布",@"我的收藏",@"我的邀请",@"客服热线"];
        }
        [_dataArray addObjectsFromArray:array];
    }
    return _dataArray;
}
-(NSArray *)imgArray{
    return @[@"wodefabu",@"wodeshoucang",@"wodeyaoqing",@"kafurexian"];
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(TSScaleW(11), self.headerView.bottom - TSScaleW(40), TSScreenWidth - TSScaleW(22), TSScreenHeight -  Height_TabBar - self.headerView.bottom + TSScaleW(40)) style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        _tableView.tableHeaderView = self.tableHeaderView;
        if (@available(iOS 11.0, *))
        {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
        }
        else
        {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
       
    }
    return _tableView;
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"UITableViewCell"];
        cell.textLabel.textColor = kMainTxtColor;
        cell.textLabel.font = [UIFont systemFontOfSize:TSScaleW(13)];
        cell.backgroundColor = kWhiteColor;
        cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"chakanquanbu"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:self.imgArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![self ifNoLoginGotoLoginAction]) {
        return;
    }
    NSString *message = self.dataArray[indexPath.row];
    if ([message isEqualToString:@"我的发布"]) {
        BkMyPublishViewController *vc = [[BkMyPublishViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([message isEqualToString:@"我的收藏"]) {
        MySavedViewController *vc = [[MySavedViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([message isEqualToString:@"我的邀请"]) {
        MyInvateViewController *vc = [[MyInvateViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([message isEqualToString:@"客服热线"]) {
//        MyServerTelViewController *vc = [[MyServerTelViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
        [self userInforRequstGetbuttonsAction];
    }
}

-(void)userInforRequstGetbuttonsAction{
    
        [NetWorkTools postConJSONWithUrl:@"getbuttons" parameters:@{} success:^(id responseObject) {
            NSString *result = responseObject[@"result"];
            NSString *resultNote = responseObject[@"resultNote"];
            NSString *datastr = responseObject[@"datastr"];
            NSString *url  = kKefuServerUrl;
            if ([result integerValue] == 0) {
                if ( [datastr integerValue] == 0) {
                    url = kKefuServerUrl;
                }else{
                    url = kSelfServerUrl;
                }
                [self gotoWebWithUrl:url title:@"我的客服"];
            }else{
                 [MBProgressHUD showError:resultNote];
            }
                         
        } fail:^(NSError *error) {
            
        }];
 
   
    
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return TSScaleW(40);
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectZero];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScaleW(44))];
    UILabel *nameLabel = [TSWLTools allocLabel:@"我的服务" font:systemBoldFont(TSScaleW(13)) textColor:kMainTxtColor frame:CGRectMake(TSScaleW(12), 0, TSScaleW(200), TSScaleW(44)) textAlignment:(NSTextAlignmentLeft)];
    UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(TSScaleW(12), TSScaleW(26), TSScaleW(50), TSScaleW(4))];
    greenView.backgroundColor = kGreenColor;
    [view addSubview:greenView];
    [view addSubview:nameLabel];
    view.backgroundColor = kWhiteColor;
    return view;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return TSScaleW(44);
}

-(void)userInforRequstAction{
    if (kLogin) {
        [NetWorkTools postConJSONWithUrl:@"memberinfo" parameters:@{} success:^(id responseObject) {
            NSString *result = responseObject[@"result"];
            NSString *resultNote = responseObject[@"resultNote"];
            NSDictionary *data = responseObject[@"dataobject"];
            if ([result integerValue] == 0) {
                LXUserInforModel *model = [[LXUserInforModel alloc]init];
                [model setValuesForKeysWithDictionary:data];
                [LXSaveUserInforTool sharedUserTool].userInforDic = data;
                self.headerView.dataDic = data;
                [kUserDefaults setValue:data[@"ismember"] forKey:@"ismember"];
            }else{
                 [MBProgressHUD showError:resultNote];
            }
                         
        } fail:^(NSError *error) {
            
        }];
    }
   
    
}

@end
